package com.k.base.jdk.tree.tree2;

import com.k.base.jdk.tree.treecov.Tree;

import java.util.ArrayList;
import java.util.List;

public class TreeTest {
    private static List<TreeNode> buildTree(List<TreeNode> nodeList){
        List<TreeNode> result = new ArrayList<>();
        for (TreeNode zone:nodeList) {
            if (zone.getPid().equals("0")) {
                result.add(zone);
                setChildren(nodeList, zone);
            }
        }
        return result;
    }

    public static void setChildren(List<TreeNode> list, TreeNode parent) {
        for (TreeNode zone: list) {
            if(parent.getId().equals(zone.getPid())){
                parent.getChildren().add(zone);
            }
        }
        if (parent.getChildren().isEmpty()) {
            return;
        }
        for (TreeNode zone: parent.getChildren()) {
            setChildren(list, zone);
        }
    }

    public static void main(String[] args) {
        List<TreeNode> simpTree = new ArrayList<>();
        TreeNode tree1 = new TreeNode();
        tree1.setId("1");
        tree1.setName("ljw1");
        tree1.setPid("0");
        simpTree.add(tree1);

        TreeNode tree12 = new TreeNode();
        tree12.setId("12");
        tree12.setName("ljw12");
        tree12.setPid("0");
        simpTree.add(tree12);

        TreeNode tree21 = new TreeNode();
        tree21.setId("21");
        tree21.setName("ljw21");
        tree21.setPid("1");
        simpTree.add(tree21);

        TreeNode tree22 = new TreeNode();
        tree22.setId("22");
        tree22.setName("ljw22");
        tree22.setPid("1");
        simpTree.add(tree22);

        TreeNode tree111 = new TreeNode();
        tree111.setId("111");
        tree111.setName("ljw111");
        tree111.setPid("21");
        simpTree.add(tree111);

        List<TreeNode> stdTree = buildTree(simpTree);
        System.out.println(stdTree);
    }
}
